<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24" style="width: 350px">
            <a-form-item label="归属人">
              <JSelectUserByDepCustom placeholder="请选择归属人" v-model="queryParam.owner"></JSelectUserByDepCustom>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="合同编号">
              <relatecontract v-model="queryParam.orderContractId" display-key='number' :query-config="hetongConfig" :url="hetongurl"
                              @change="hetongChange"  />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="回款日期">
              <j-date placeholder="请选择回款日期" v-model="queryParam.collectionDate"></j-date>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('回款单')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
    </div>

    <!-- table区域-begin -->
    <div>
      <div>
        <a-tabs default-active-key=" " @change="tabsChange">
          <a-tab-pane key=" " tab="全部" force-render></a-tab-pane>
          <a-tab-pane key="0" tab="本周回款"></a-tab-pane>
          <a-tab-pane key="1" tab="本月回款"></a-tab-pane>
          <a-tab-pane key="2" tab="上月回款"></a-tab-pane>
        </a-tabs>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">
        //客户详情
        <template slot="customerIdDictText" slot-scope="text,record">
          <a @click="seeCustomerId(record)">{{ text }}</a>
        </template>
<!--        合同详情-->
        <template slot="orderContractId_dictText" slot-scope="text,record">
          <a @click="seeAgreementId(record)">{{ text }}</a>
        </template>
<!--        应收款详情-->
        <template slot="receivablesIds_dictText" slot-scope="text,record">
          <a @click="seeReceivablesId(record)">{{ text }}</a>
        </template>

        <template slot="number" slot-scope="text, record">
          <a v-html="text" @click="handleDetail(record)" color="blue"></a>
        </template>
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a v-has="'payment:delete'">删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
      <div class="down_bottom">
        <div class="ant-alert ant-alert-info">
          <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
          <a-divider class="btn" type="vertical" />
          <a  @click="onClearSelected">清空</a>
          <a-button  type="default" @click="MAction(false,'/payment/paymentCollection/updateUserId',1,1260)" v-has="'payment:transfer_user'">移交</a-button>
          <a-button  type="default" @click="MAction(false,'/payment/paymentCollection/updateDirector',2,1260)">添加负责人</a-button>
          <a-button  type="default" @click="MAction(false,'/payment/paymentCollection/deleteDirector',2,1260)">删除负责人</a-button>
          <a-button  type="default" @click="MAction(true,'/payment/paymentCollection/updateDirector',3,1260)">添加协同人</a-button>
          <a-button  type="default" @click="MAction(true,'/payment/paymentCollection/deleteDirector',3,1260)">删除协同人</a-button>
          <a-button  type="danger"  @click="batchDel" v-has="'payment:batch_delete'">删除</a-button>
        </div>
      </div>
    </div>
    <payment-collection-modal ref="modalForm" @ok="modalFormOk"></payment-collection-modal>
    <CustomerModal ref="CustomerModal"></CustomerModal>
    <ContractOrderModal ref="ContractOrderModal" ></ContractOrderModal>
    <ReceivablesModal ref="ReceivablesModal" ></ReceivablesModal>
    <JSelectUserByDepModalCustom ref="MAmodel" :modalWidth="MAwidth" :multi="MAisMulti" @ok="MACallback"></JSelectUserByDepModalCustom>

  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import PaymentCollectionModal from './modules/PaymentCollectionModal.Style#Drawer.vue'
  import {filterMultiDictText} from '@/components/dict/JDictSelectUtil'
  import { getAction, httpAction, postAction } from '@api/manage'
  import CustomerModal from '@views/customer/modules/CustomerModal.Style#Drawer'
  import ContractOrderModal from '@views/ContractOrderList/modules/ContractOrderModal.Style#Drawer'
  import ReceivablesModal from '@views/receivables/modules/ReceivablesModal.Style#Drawer'
  import JSelectUserByDepModalCustom from '@comp/jeecgbiz/modal/JSelectUserByDepModalCustom'
  import JSelectUserByDepCustom from '@/components/jeecgbiz/JSelectUserByDepCustom'
  import relatecontract from '@/components/jeecgbiz/relatecontract/index'

  export default {
    name: 'PaymentCollectionList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      PaymentCollectionModal,
      CustomerModal,
      ContractOrderModal,
      ReceivablesModal,
      JSelectUserByDepModalCustom,
      JSelectUserByDepCustom,
      relatecontract
    },
    data () {
      return {
        hetongurl: '/contractorder/contractOrder/list?filed=2&customerId=' ,
        hetongConfig: [
          { key: 'number', label: '合同编号' }
        ],
        //名单选择时 多选还是单选
        MAisMulti:false,
        //选择名单回调接口
        MAcallBackUrl:'',
        //名单参数类型    1===移交 2===负责人 3协同人 4===分配
        MAtype:0,
        //弹框宽度
        MAwidth:1260,
        description: '回款单管理页面',
        // 表头
        columns: [
          {
            title:'类型',
            align:"center",
            dataIndex: 'collectionType_dictText',
          },
          {
            title:'回款单编号',
            align:"center",
            dataIndex: 'number',
            scopedSlots: { customRender: 'number' },
          },
          {
            title:'关联客户',
            align:"center",
            dataIndex: 'customerIdDictText',
            scopedSlots: { customRender: 'customerIdDictText' },
          },
          {
            title:'关联合同',
            align:"center",
            dataIndex: 'orderContractId_dictText',
            scopedSlots: { customRender: 'orderContractId_dictText' },
          },
          {
            title:'关联应收款',
            align:"center",
            dataIndex: 'receivablesIds_dictText',
            scopedSlots: { customRender: 'receivablesIds_dictText' },
          },
          {
            title:'回款金额',
            align:"center",
            dataIndex: 'collectionAmount'
          },
          {
            title:'核销金额',
            align:"center",
            dataIndex: 'writeOffAmount'
          },
          {
            title:'归属人',
            align:"center",
            dataIndex: 'owner_dictText'
          },
          {
            title:'回款日期',
            align:"center",
            dataIndex: 'collectionDate',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'收款账户',
            align:"center",
            dataIndex: 'collectionAccount_dictText'
          },
          {
            title:'支付方式',
            align:"center",
            dataIndex: 'paymentMethod_dictText'
          },
          {
            title:'备注',
            align:"center",
            dataIndex: 'remrak'
          },
          {
            title:'附件',
            align:"center",
            dataIndex: 'files',
            scopedSlots: {customRender: 'fileSlot'}
          },
          {
            title:'创建人',
            align:"center",
            dataIndex: 'createBy'
          },
          {
            title:'创建时间',
            align:"center",
            dataIndex: 'createTime'
          },
          {
            title:'更新时间',
            align:"center",
            dataIndex: 'updateTime'
          },
          {
            title: '负责人',
            align: 'center',
            dataIndex: 'directorIds_dictText'
          },
          {
            title: '协同人',
            align: 'center',
            dataIndex: 'collaboratorIds_dictText'
          },

          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/payment/paymentCollection/list",
          customList: '/customer/customer/list',
          contractList: "/contractorder/contractOrder/list",
          receivablesList: "/receivables/receivables/list",
          delete: "/payment/paymentCollection/delete",
          deleteBatch: "/payment/paymentCollection/deleteBatch",
          exportXlsUrl: "/payment/paymentCollection/exportXls",
          importExcelUrl: "payment/paymentCollection/importExcel",
          updateUserId:"/payment/paymentCollection/updateUserId",//移交
          updateDirector:"/payment/paymentCollection/updateDirector",//添加
          deleteDirector:"/payment/paymentCollection/deleteDirector",//删除

        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
    this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'number',text:'编号',dictCode:''})
        fieldList.push({type:'sel_search',value:'customerId',text:'客户ID',dictTable:"biz_customer", dictText:'name', dictCode:'id'})
        fieldList.push({type:'list_multi',value:'contactsIds',text:'联系人IDS',dictTable:"biz_contacts", dictText:'name', dictCode:'id'})
        fieldList.push({type:'list_multi',value:'receivablesIds',text:'应收款IDS',dictTable:"biz_receivables", dictText:'number', dictCode:'id'})
        fieldList.push({type:'string',value:'owner',text:'归属人',dictCode:''})
        fieldList.push({type:'date',value:'collectionDate',text:'回款日期'})
        fieldList.push({type:'int',value:'collectionAccount',text:'回款账户(1默认账户)',dictCode:'collection_account'})
        fieldList.push({type:'int',value:'paymentMethod',text:'支付方式(1.网银转账 2.现金 3.支票 4.电汇 5.承兑汇票)',dictCode:'collection_payment_method'})
        fieldList.push({type:'string',value:'remrak',text:'备注',dictCode:''})
        fieldList.push({type:'string',value:'files',text:'附件',dictCode:''})
        this.superFieldList = fieldList
      },
      MAction(MAisMulti,MAcallBackUrl,MAtype,MAwidth){
        //Member action
        //设置多选还是单选
        // this.MAisMulti = false
        this.MAisMulti = MAisMulti
        //设置回调接口
        // this.MAcallBackUrl  = '/quotation/quotation/updateDirector'
        this.MAcallBackUrl  = MAcallBackUrl
        //设置回调接口参数
        // this.MAtype = 1
        this.MAtype = MAtype
        //弹框宽度
        // this.MAwidth = 1260
        this.MAwidth = MAwidth
        if (this.selectedRowKeys.length <= 0) {
          this.$message.warning('请至少或者最多选择一条记录！');
          return;
        }
        this.$refs.MAmodel.showModal()
      },
      MACallback(e){
        this.loading = true
        console.log('e',e)
        console.log('this.selectedRowKeys',this.selectedRowKeys)
        let tempArr = e;
        let idArr = tempArr.map(e => {
          return e.id
        })
        let collaboratorIds_idArr = idArr.join(',')
        let ids = this.selectedRowKeys.join(',')
        let obj = {}
        switch (this.MAtype) {
          case 1://移交
            obj = {id:ids,userId:e[0].id}
            break;
          case 2://负责人
            obj = {id:ids,directorIds:e[0].id}
            break;
          case 3://协同人
            obj = {id:ids,collaboratorIds:collaboratorIds_idArr}
            break;
          case 4://分配
            obj = {id:ids,directorIds:e[0].id}
            break;

        }
        postAction(this.MAcallBackUrl,obj).then(res=>{
          console.log('移交后',res)
          if(res.success){
            this.selectedRowKeys = []
            this.$message.success(res.message)
            this.loading = false
            this.loadData()
          }else {
            this.$message.error(res.message)
            this.loading = false
            // this.loadData()
          }

        })
      },
      //查看客户详情
      seeCustomerId(record) {
        this.loading = true
        getAction(this.url.customList, { id: record.customerId }).then(res => {
          if (res.success) { this.$refs.CustomerModal.dongtai(record.customerId)
            this.$refs.CustomerModal.dongtaiShow = true
            this.$refs.CustomerModal.edit(res.result.records[0])
            this.$refs.CustomerModal.disableSubmit = true
          } else {
            this.$message.error(res.message)
          }
        }).finally(() => {
          this.loading = false
        })
      },
      //查看合同详情
      seeAgreementId(record) {
        console.log('htttttttttttttttttttttttttttttt',record)
        this.loading = true
        getAction(this.url.contractList, { id: record.orderContractId }).then(res => {
          if (res.result.records.length <= 0) {
            this.$message.error('无权查看！')
            return false
          }
          if (res.success) {
            this.$refs.ContractOrderModal.edit(res.result.records[0])
            this.$refs.ContractOrderModal.disableSubmit = true
          } else {
            this.$message.error(res.message)
          }
        }).finally(() => {
          this.loading = false
        })
      },
      //查看应收款详情
      seeReceivablesId(record) {
        this.loading = true
        console.log('yskkkkkkkkkkkkkkkkkkkkkkkkkkkkkk',record)
        getAction(this.url.receivablesList, { id: record.receivablesIds }).then(res => {
          if (res.result.records.length <= 0) {
            this.$message.error('无权查看！')
            return false
          }
          if (res.success) {
            this.$refs.ReceivablesModal.edit(res.result.records[0])
            this.$refs.ReceivablesModal.disableSubmit = true
          } else {
            this.$message.error(res.message)
          }
        }).finally(() => {
          this.loading = false
        })
      },
      //tab页查询
      tabsChange(e) {
        console.log(e)
        if (e == '') {
          this.queryParam = {}
          this.loadData()
        } else {
          this.queryParam.statusTop = e
          this.loadData()
        }
      },
      handleEdit: function (record) {
        this.$refs.modalForm.edit(record);
        this.$refs.modalForm.title = "编辑";
        this.$refs.modalForm.disableSubmit = false;
      },
      hetongChange(e) {
        console.log(e)
      },
    }
  }
</script>
<style scoped lang="less">
@import '~@assets/less/common.less';
.down_bottom{
  button{
    margin-left: 10px;
  }
}
</style>